<?php

function reequipment_user_equipment()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:create:delete:edit:update:overview:maintenance:pending:report', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('equipment_id', 'int:1:', $equipment_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('model_id', 'int:1:', $model_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('area_id', 'int:1:', $area_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('serial_number', 'str:1:', $serial_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('voltage', 'str:1:', $voltage, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('cycles', 'int:1:', $cycles, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('machine_name', 'str:1:', $machine_name, '', XARVAR_NOT_REQUIRED)) return;
    xarTplSetPageTitle(xarML('Equipment'));
    if (!xarSecurityCheck('EditEquipment', 1)) return;
    $data = array();
     // Get company_id
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;
	if (!xarModAPIFunc('reequipment', 'user', 'getcompanyid', array('company_id_from_url' => $company_id))) return;
	$company_id = xarSessionGetVar('company_id');

    switch(strtolower($phase)) {

        case 'create':
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createequipment', array('model_id' => $model_id, 'company_id' => $company_id, 'serial_number' => $serial_number, 'voltage' => $voltage, 'cycles' => $cycles, 'machine_name' => $machine_name));
        break;

        case 'delete':
        		// return $equipment_id;
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'updateequipment', array('equipment_id' => $equipment_id, 'company_id' => $company_id, 'inactive' => true));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'main'));
        break;

        case 'edit':
        	// Tell template we are working on multiple machines
        	$data['multiple'] = true;

            $data['models'] = xarModAPIFunc('reequipment', 'user', 'getmodels');
            $data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('company_id' => $company_id));
            // return var_export($data['equipment']);
            // Company Name
            $companies = xarModAPIFunc('reequipment', 'user', 'getrel', array('company_id' => $company_id));
            foreach($companies as $var){
            	$data['company_name'] = $var['company_name'];
            }
            // return var_export($data['schedules']);
            $data['form_label'] = xarML('Edit Equipment');
            $data['form_phase'] = 'update';
            $form_var = xarModAPIFunc('reequipment', 'user', 'getequipment', array('equipment_id' => $equipment_id, 'company_id' => $company_id));
            // return var_export($form_var);
            foreach($form_var as $var){
				$data['form_model_id'] = $var['model_id'];
				$data['form_company_id'] = $var['company_id'];
				$data['form_serial_number'] = $var['serial_number'];
				$data['form_equipment_id'] = $var['equipment_id'];
				$data['form_area_id'] = $var['area_id'];
				$data['form_cycles'] = $var['cycles'];
				$data['form_voltage'] = $var['voltage'];
				$data['form_machine_name'] = $var['machine_name'];
				$data['voltage_selected_na'] = '';
				$data['voltage_selected_110'] = '';
				$data['voltage_selected_208'] = '';
				$data['voltage_selected_240'] = '';
				$data['voltage_selected_480'] = '';
				$data['voltage_selected_560'] = '';
				if ($var['voltage'] == 'Not Applicable'){
					$data['voltage_selected_na'] = 'selected="selected"';
				} elseif ($var['voltage'] == '208'){
					$data['voltage_selected_208'] = 'selected="selected"';
				} elseif ($var['voltage'] == '240'){
					$data['voltage_selected_240'] = 'selected="selected"';
				} elseif ($var['voltage'] == '480'){
					$data['voltage_selected_480'] == 'selected="selected"';
				} elseif ($var['voltage'] = '110'){
					$data['voltage_selected_110'] == 'selected="selected"';
				} elseif ($var['voltage'] = '560'){
					$data['voltage_selected_560'] == 'selected="selected"';
				}
				// Need to add Canadian Voltage
			}
        break;

		case 'update':
			if (!is_int($cycles) or !is_int($model_id)) {
        		return false;
    		}
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateequipment', array('equipment_id' => $equipment_id, 'model_id' => $model_id, 'serial_number' => $serial_number, 'voltage' => $voltage, 'cycles' => $cycles, 'machine_name' => $machine_name));
			// xarResponseRedirect(xarModURL('reequipment', 'user', 'equipment', array('company_id' => $company_id)));
			return true;
        break;

        case 'overview':
        	// Tell template that we are working on a single machine.
            $data['single'] = true;

            $data['models'] = xarModAPIFunc('reequipment', 'user', 'getmodels');
            $data['form_label'] = xarML('Edit Equipment');
            $data['form_phase'] = 'update';
        	$data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('company_id' => $company_id, 'equipment_id' => $equipment_id));
        	foreach($data['equipment'] as $var){
				$data['form_model_id'] = $var['model_id'];
				$data['form_company_id'] = $var['company_id'];
				$data['form_serial_number'] = $var['serial_number'];
				$data['form_equipment_id'] = $var['equipment_id'];
				$data['form_area_id'] = $var['area_id'];
				$data['form_cycles'] = $var['cycles'];
				$data['form_voltage'] = $var['voltage'];
				$data['form_machine_name'] = $var['machine_name'];
				$data['voltage_selected_na'] = '';
				$data['voltage_selected_110'] = '';
				$data['voltage_selected_208'] = '';
				$data['voltage_selected_240'] = '';
				$data['voltage_selected_480'] = '';
				$data['voltage_selected_560'] = '';
				if ($var['voltage'] == 'Not Applicable'){
					$data['voltage_selected_na'] = 'selected="selected"';
				} elseif ($var['voltage'] == '208'){
					$data['voltage_selected_208'] = 'selected="selected"';
				} elseif ($var['voltage'] == '240'){
					$data['voltage_selected_240'] = 'selected="selected"';
				} elseif ($var['voltage'] == '480'){
					$data['voltage_selected_480'] == 'selected="selected"';
				} elseif ($var['voltage'] = '110'){
					$data['voltage_selected_110'] == 'selected="selected"';
				} elseif ($var['voltage'] = '560'){
					$data['voltage_selected_560'] == 'selected="selected"';
				}
				// Need to add Canadian Voltage
			}
			// return var_export($data);

        break;
    }
    // Return the output
    return $data;
}

?>